<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Hugo v0.11 introduced the concept of a content builder. Using the command: hugo new [relative new content path], you can start a content file with the date and title automatically set. While this is a welcome feature, active writers need more.
Hugo presents the concept of archetypes, which are archetypal content files with pre-configured front matter which will populate each new content file whenever you run the hugo new command.">
        <link rel="alternate" href="https://www.feiyangkeji.com/feed.xml" type="application/rss+xml" title="阳阳学编程">
        <link rel="icon" href="https://www.feiyangkeji.com/favicon.ico">
        <title>Archetypes - 阳阳学编程</title>
        <link rel="stylesheet" href="https://www.feiyangkeji.com/css/highlight.js.min.css">
        <link rel="stylesheet" href="https://www.feiyangkeji.com/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://www.feiyangkeji.com/css/bootstrap-theme.css">
        <link rel="stylesheet" href="https://www.feiyangkeji.com/css/font-awesome/css/font-awesome.min.css" />
        
        <link rel="stylesheet" href="https://www.feiyangkeji.com/css/hugo-org.css">
    </head>

<body role="document">
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="https://www.feiyangkeji.com/">阳阳学编程</a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    
                    <li ><a href="https://www.feiyangkeji.com/">主页</a></li>
                    <li ><a href="https://www.feiyangkeji.com/doc">固件下载</a></li>
                   
                    
                    <li ><a href="https://www.feiyangkeji.com/post/">文章列表</a></li>
                    
                    <li ><a href="https://www.feiyangkeji.com/theme/">开源项目</a></li>
                    
                    <li ><a href="https://www.feiyangkeji.com/showcase/">免费资源</a></li>
                    
                    <li ><a href="https://www.feiyangkeji.com/donate/">关于</a></li>
                    

                </ul>
            </div>
        </div>
    </nav>



<div class="container-fluid">

<div class="row">
    <div class="col-xs-12 col-sm-3">
        
<aside>
    <div class="well">
        
        <div class="pull-right">智能开关固件 v2.0.1(开发中...)</div>
        <ul class="nav nav-list">
            
            
            
            <li>
                <label class="tree-toggle nav-header">快速开始</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/overview/quickstart/">Quickstart</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">内容(Content)</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/content/archetypes/">Archetypes</a>
                    </li>
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/content/example_en/">Example</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">分类(Taxonomies)</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/taxonomies/displaying/">Displaying</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">常见问题</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/troubleshooting/overview/">Troubleshooting Overview</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">关于Hugo</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/meta/license/">License</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">community</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/community/mailing-list/">Mailing List</a>
                    </li>
                    
                    <li>
                        <a href="http://discuss.gohugo.io">讨论社区</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">extras</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/extras/shortcodes/">Shortcodes</a>
                    </li>
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/extras/toc_en/">Table of Contents</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">layout</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/templates/terms_en/">Taxonomy Terms</a>
                    </li>
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/templates/sitemap/">Sitemap</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">themes</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/themes/creation/">Creating a Theme</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
            <li>
                <label class="tree-toggle nav-header">tutorials</label>
                <ul class="nav nav-list">
                    
                    <li>
                        <a href="https://www.feiyangkeji.com/doc/tutorials/automated-deployments/">Automated deployments</a>
                    </li>
                    
                </ul>
            
            </li>
            
            
              
            <li>
                <a href="https://github.com/coderzh/feiyangkeji.com/edit/master/content/doc/content/archetypes.md" target="blank">
                <i class='fa fa-edit'></i>修改此文章</a>
            </li>
            
            
        </ul>
    </div>
</aside>


    </div>
    <div class="col-xs-12 col-sm-9 doc-main">
        <main role="main">
            <article>
                <a id="title"></a>
                <h1 class="doc-entry-title">Archetypes</h1>
                <section>
                    <p>Hugo v0.11 introduced the concept of a content builder. Using the
command: <!-- raw HTML omitted -->hugo new <!-- raw HTML omitted -->[relative new content path]<!-- raw HTML omitted --><!-- raw HTML omitted -->,
you can start a content file with the date and title automatically set.
While this is a welcome feature, active writers need more.</p>
<p>Hugo presents the concept of archetypes, which are archetypal content files
with pre-configured <a href="https://www.feiyangkeji.com/doc/content/front-matter">front matter</a> which will
populate each new content file whenever you run the <code>hugo new</code> command.</p>
<h2 id="example">Example</h2>
<h3 id="step-1-creating-an-archetype">Step 1. Creating an archetype</h3>
<p>In this example scenario, we have a blog with a single content type (blog post).
We will use ‘tags’ and ‘categories’ for our taxonomies, so let&rsquo;s create an archetype file with ‘tags’ and ‘categories’ pre-defined, as follows:</p>
<h4 id="archetypesdefaultmd">archetypes/default.md</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-toml" data-lang="toml"><span style="color:#960050;background-color:#1e0010">+++</span>
<span style="color:#a6e22e">tags</span> = [<span style="color:#e6db74">&#34;x&#34;</span>, <span style="color:#e6db74">&#34;y&#34;</span>]
<span style="color:#a6e22e">categories</span> = [<span style="color:#e6db74">&#34;x&#34;</span>, <span style="color:#e6db74">&#34;y&#34;</span>]
<span style="color:#960050;background-color:#1e0010">+++</span>
</code></pre></div><blockquote>
<p><strong>CAVEAT:</strong>  Some editors (e.g. Sublime, Emacs) do not insert an EOL (end-of-line) character at the end of the file (i.e. EOF).  If you get a <a href="https://www.feiyangkeji.com/doc/troubleshooting/strange-eof-error/">strange EOF error</a> when using <code>hugo new</code>, please open each archetype file (i.e. <code>archetypes/*.md</code>) and press <!-- raw HTML omitted -->Enter<!-- raw HTML omitted --> to type a carriage return after the closing <code>+++</code> or <code>---</code> as necessary.</p>
</blockquote>
<h3 id="step-2-using-the-archetype">Step 2. Using the archetype</h3>
<p>Now, with <code>archetypes/default.md</code> in place, let&rsquo;s create a new post in the <code>post</code> section with the <code>hugo new</code> command:</p>
<pre><code>$ hugo new post/my-new-post.md
</code></pre>
<p>Hugo would create the file with the following contents:</p>
<h4 id="doccontentpostmy-new-postmd">doc/content/post/my-new-post.md</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-toml" data-lang="toml"><span style="color:#960050;background-color:#1e0010">+++</span>
<span style="color:#a6e22e">title</span> = <span style="color:#e6db74">&#34;my new post&#34;</span>
<span style="color:#a6e22e">date</span> = <span style="color:#e6db74">&#34;2015-01-12T19:20:04-07:00&#34;</span>
<span style="color:#a6e22e">tags</span> = [<span style="color:#e6db74">&#34;x&#34;</span>, <span style="color:#e6db74">&#34;y&#34;</span>]
<span style="color:#a6e22e">categories</span> = [<span style="color:#e6db74">&#34;x&#34;</span>, <span style="color:#e6db74">&#34;y&#34;</span>]
<span style="color:#960050;background-color:#1e0010">+++</span>
</code></pre></div><p>We see that the <code>title</code> and <code>date</code> variables have been added, in addition to the <code>tags</code> and <code>categories</code> variables which were carried over from <code>archetype/default.md</code>.</p>
<p>Congratulations!  We have successfully created an archetype and used it for our new contents. But wait, what if I want to have different variables for another content type, like musicians? No problem.</p>
<h3 id="creating-custom-archetypes">Creating custom archetypes</h3>
<p>Earlier you created a new content type by adding a new subfolder to the content directory. In our case it&rsquo;s name would be <code>content/musician</code>. To use the corresponding archetype you just need to create a file named after the content type called <code>musician.md</code> in the <code>archetypes</code> directory, similar to the one below.</p>
<h4 id="archetypesmusicianmd">archetypes/musician.md</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-toml" data-lang="toml"><span style="color:#960050;background-color:#1e0010">+++</span>
<span style="color:#a6e22e">name</span> = <span style="color:#e6db74">&#34;&#34;</span>
<span style="color:#a6e22e">bio</span> = <span style="color:#e6db74">&#34;&#34;</span>
<span style="color:#a6e22e">genre</span> = <span style="color:#e6db74">&#34;&#34;</span>
<span style="color:#960050;background-color:#1e0010">+++</span>
</code></pre></div><p>Now let&rsquo;s create a new musician.</p>
<pre><code>$ hugo new musician/mozart.md
</code></pre>
<p>This time, Hugo recognizes the custom archetype and uses it instead of the default one. So the generated file&rsquo;s frontmatter now includes the variables <code>name</code>, <code>bio</code> and <code>genre</code>.</p>
<h4 id="doccontentmusicianmozartmd">doc/content/musician/mozart.md</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-toml" data-lang="toml"><span style="color:#960050;background-color:#1e0010">+++</span>
<span style="color:#a6e22e">title</span> = <span style="color:#e6db74">&#34;mozart&#34;</span>
<span style="color:#a6e22e">date</span> = <span style="color:#e6db74">&#34;2015-08-24T13:04:37+02:00&#34;</span>
<span style="color:#a6e22e">name</span> = <span style="color:#e6db74">&#34;&#34;</span>
<span style="color:#a6e22e">bio</span> = <span style="color:#e6db74">&#34;&#34;</span>
<span style="color:#a6e22e">genre</span> = <span style="color:#e6db74">&#34;&#34;</span>
<span style="color:#960050;background-color:#1e0010">+++</span>
</code></pre></div><h2 id="using-a-different-front-matter-format">Using a different front matter format</h2>
<p>By default, the front matter will be created in the TOML format
regardless of what format the archetype is using.</p>
<p>You can specify a different default format in your site-wide config file
(e.g. <code>config.toml</code>) using the <code>MetaDataFormat</code> directive.
Possible values are <code>&quot;toml&quot;</code>, <code>&quot;yaml&quot;</code> and <code>&quot;json&quot;</code>.</p>
<h2 id="which-archetype-is-being-used">Which archetype is being used</h2>
<p>The following rules apply:</p>
<ul>
<li>If an archetype with a filename that matches the content type being created, it will be used.</li>
<li>If no match is found, <code>archetypes/default.md</code> will be used.</li>
<li>If neither is present and a theme is in use, then within the theme:
<ul>
<li>If an archetype with a filename that matches the content type being created, it will be used.</li>
<li>If no match is found, <code>archetypes/default.md</code> will be used.</li>
</ul>
</li>
<li>If no archetype files are present, then the one that ships with Hugo will be used.</li>
</ul>
<p>Hugo provides a simple archetype which sets the <code>title</code> (based on the
file name) and the <code>date</code> in RFC 3339 format based on
<a href="http://golang.org/pkg/time/#Now"><code>now()</code></a>, which returns the current time.</p>
<blockquote>
<p><em>Note: <code>hugo new</code> does not automatically add <code>draft = true</code> when the user
provides an archetype.  This is by design, rationale being that
the archetype should set its own value for all fields.
<code>title</code> and <code>date</code>, which are dynamic and unique for each piece of content,
are the sole exceptions.</em></p>
</blockquote>
<p>Content type is automatically detected based on the path. You are welcome to declare which type to create using the <code>--kind</code> flag during creation.</p>

                </section>
            </article>
        </main>
        <br />
        
        <div class="row">
            <div class="col-xs-6 col-sm-6 text-center">
                
                <a href="https://www.feiyangkeji.com/doc/content/types">&lt; 上一篇</a>
                
            </div>
            <div class="col-xs-6 col-sm-6 text-center">
                
                <a href="https://www.feiyangkeji.com/doc/content/ordering">下一篇 &gt;</a>
                
            </div>
        </div>
        
    </div> 
</div>



<hr />

<div class="row">
    <div class="col-sm-12">
        
	    <div class="text-center">
        
		    <p class="doc-footer-em"><a href="#">回到顶部↑</a></p>
	    </div>
	</div>
</div>

</div> 

<footer class="doc-footer">
	
    <p>Copyright © <a href="http://www.feiyangkeji.com/"> 北京飞阳信远科技有限公司(京ICP备2021020257号-1 )</a> 2021. Generated by <a href="http://gohugo.io/">Hugo</a></p>
</footer>

<script>
	(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
	(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
	ga('create', 'UA-10147768-3', 'auto');
	ga('send', 'pageview');
</script>

<script src="https://www.feiyangkeji.com/js/jquery-1.11.2.min.js"></script>
<script src="https://www.feiyangkeji.com/js/bootstrap.min.js"></script>

<script src="https://www.feiyangkeji.com/js/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="https://www.feiyangkeji.com/js/ie10-viewport-bug-workaround.js"></script>

</body>
</html>

